using Engine;
using Game;

namespace SCIENEW.VoltNet {
    /// <summary>
    /// 电力单元的类
    /// </summary>
    public abstract class VoltElement {
        public SubsystemVoltNet SubsystemVoltNet;
        public SubsystemTerrain SubsystemTerrain;
        public Point3 Position;
        public int BlockValue;

        // 工作状态
        public WorkState CurrentWorkState { get; protected set; }

        public VoltElement(SubsystemVoltNet subsystemVoltNet, SubsystemTerrain subsystemTerrain, Point3 position, int blockValue) {
            SubsystemTerrain = subsystemTerrain;
            SubsystemVoltNet = subsystemVoltNet;
            Position = position;
            BlockValue = blockValue;
            CurrentWorkState = WorkState.Underpowered; // 初始化为欠功率状态
        }

        /// <summary>
        /// 获取实际的额定功率
        /// </summary>
        /// <param name="standardPL"></param>
        /// <returns></returns>
        public virtual float GetPL(float standardPL, CellFace cellFace) => standardPL;

        /// <summary>
        /// 获取实际的额定电压
        /// </summary>
        /// <param name="standardUL"></param>
        /// <returns></returns>
        public virtual float GetUL(float standardUL, CellFace cellFace) => standardUL;

        public virtual void OnAdded() { }

        public virtual void OnRemoved() { }

        // 更新工作状态
        public virtual void UpdateWorkState(float inputPower, float inputVoltage, float requiredPower, float requiredVoltage) {
            if (inputVoltage < requiredVoltage) {
                CurrentWorkState = WorkState.Underpowered;
            } else if (inputVoltage == requiredVoltage) {
                if (inputPower < requiredPower) {
                    CurrentWorkState = WorkState.Underpowered;
                } else if (inputPower == requiredPower) {
                    CurrentWorkState = WorkState.Active;
                } else {
                    CurrentWorkState = WorkState.Overloaded;
                }
            } else {
                CurrentWorkState = WorkState.Overloaded;
            }
        }

    }
}